package ee.falcon.gui.frame.actionlisteners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ee.falcon.gui.frame.NewOrEditProfileFrame;
import ee.falcon.gui.frame.ProfilesFrame;
import ee.falcon.xml.GreatestIdDetector;
import ee.falcon.xml.ProfileInfoList;
import ee.falcon.xml.ProfileInfoXMLModifier;
import ee.falcon.xml.SpecificProfileInfo;

public class SaveButtonListener implements ActionListener
{
	private NewOrEditProfileFrame newOrEditProfileFrame;
	private boolean is_edit_profile_frame; 
	private SpecificProfileInfo editableProfile; 
	private SpecificProfileInfo newProfile; 
	private ProfileInfoXMLModifier profileInfoXMLModifier; 
	private ProfilesFrame profile; 
	private ProfileInfoList profileInfoList; 

	public SaveButtonListener(NewOrEditProfileFrame newOrEditProfileFrame, 
			ProfileInfoXMLModifier profileInfoXMLModifier, 
			ProfileInfoList profileInfoList, ProfilesFrame profile)
	{
		this.newOrEditProfileFrame = newOrEditProfileFrame; 
		this.is_edit_profile_frame = newOrEditProfileFrame.get_is_edit_profile_frame(); 
		this.editableProfile = newOrEditProfileFrame.getEditableProfile(); 
		this.newProfile = newOrEditProfileFrame.getNewProfile(); 
		this.profileInfoXMLModifier = profileInfoXMLModifier; 
		this.profileInfoList = profileInfoList; 
		this.profile = profile; 
	}
	
	@Override
	public void actionPerformed(ActionEvent saveButtonEvent) 
	{
		newOrEditProfileFrame.validateInputs();
		if (newOrEditProfileFrame.getIsValidationSuccessful())
		{	
			if (is_edit_profile_frame)
			{
				editableProfile.setHost(newOrEditProfileFrame.getHostNamejTextField().getText()); 
				editableProfile.setUser(newOrEditProfileFrame.getUserNameJTextField().getText());
				editableProfile.setPassword(newOrEditProfileFrame.getJPasswordFieldValue()); 
				editableProfile.setPort(Integer.parseInt(newOrEditProfileFrame.
						getPortNumberJSpinner().getValue().toString())); 
				editableProfile.setProtocol(newOrEditProfileFrame.getFileProtocolJComboBox().
						getSelectedItem().toString()); 
				editableProfile.setProtocolEncryptionType(newOrEditProfileFrame.getEncryptionTypeJComboBox().
						getSelectedItem().toString());
				
				profileInfoXMLModifier = new ProfileInfoXMLModifier();
				
				profileInfoXMLModifier.updateProfileInProfileInfoXML(editableProfile);  
			}
			else
			{
				newProfile = new SpecificProfileInfo(); 
				
				newProfile.setHost(newOrEditProfileFrame.getHostNamejTextField().getText()); 
				newProfile.setUser(newOrEditProfileFrame.getUserNameJTextField().getText());
				newProfile.setPassword(newOrEditProfileFrame.getJPasswordFieldValue()); 
				newProfile.setPort(Integer.parseInt(newOrEditProfileFrame.
						getPortNumberJSpinner().getValue().toString())); 
				newProfile.setProtocol(newOrEditProfileFrame.getFileProtocolJComboBox().
						getSelectedItem().toString()); 
				newProfile.setProtocolEncryptionType(newOrEditProfileFrame.getEncryptionTypeJComboBox().
						getSelectedItem().toString());
				
				
				profileInfoXMLModifier = new ProfileInfoXMLModifier();
				profileInfoXMLModifier.writeNewProfileToProfileInfoXML(newProfile, new GreatestIdDetector());
			}	
		
			//Now lets update JList and HasMap data aswell
			profileInfoList.renewData();
			profile.getProfilesFrameJListEditor().renewJListData(); 
			profile.setEnabled(true); 
			newOrEditProfileFrame.setVisible(false); 
			newOrEditProfileFrame.dispose();
		}	
	}
}